package com.ruoyi.javid.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.javid.mapper.ToolsExpiredWarnningMapper;
import com.ruoyi.javid.pojo.ToolsExpiredWarnning;
import com.ruoyi.javid.vo.ExpiredWarnningForm;
import com.ruoyi.javid.vo.ExpiredWarnningList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ToolsExpiredWarnningService {

    @Autowired
    ToolsExpiredWarnningMapper toolsExpiredWarnningMapper;

    public AjaxResult expiredWarnning(ExpiredWarnningForm expiredWarnningForm) {
        ToolsExpiredWarnning toolsExpiredWarnning = new ToolsExpiredWarnning();
        toolsExpiredWarnning.setName(expiredWarnningForm.getName());
        toolsExpiredWarnning.setProductDate(expiredWarnningForm.getProductDate());
        toolsExpiredWarnning.setEndDate(expiredWarnningForm.getEndDate());
        toolsExpiredWarnning.setDaysNum(expiredWarnningForm.getDaysNum());
        toolsExpiredWarnning.setDaysType(expiredWarnningForm.getDaysType());
        Date date = new Date();
        toolsExpiredWarnning.setCreateTime(date);
        toolsExpiredWarnning.setUpdateTime(date);
        int insert = toolsExpiredWarnningMapper.insert(toolsExpiredWarnning);
        if (insert > 0) {
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("sql出错");
    }

    public List<ExpiredWarnningList> expiredWarnningList() {
        try {
            List<ExpiredWarnningList> resultList = new ArrayList<>();
            QueryWrapper<ToolsExpiredWarnning> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("state", 0);
            List<ToolsExpiredWarnning> list = toolsExpiredWarnningMapper.selectList(queryWrapper);
            for (ToolsExpiredWarnning t : list) {
                try {
                    ExpiredWarnningList expiredWarnningList = new ExpiredWarnningList();
                    expiredWarnningList.setId(t.getId());
                    expiredWarnningList.setName(t.getName());
                    Date endDate;
                    if (t.getEndDate() != null) {
                        endDate = t.getEndDate();
                    } else {
                        // 生产日期加保质期得到过期日期
                        Integer daysNum = t.getDaysNum();
                        Integer daysType = t.getDaysType();
                        Integer sumDays;
                        if (daysType == 1) {
                            sumDays = daysNum;
                        } else if (daysType == 2) {
                            sumDays = daysNum * 30;
                        } else if (daysType == 3) {
                            sumDays = daysNum * 365;
                        } else {
                            throw new Exception("数据存储有误-计量单位不存在");
                        }
                        endDate = DateUtils.addDay(t.getProductDate(), sumDays);
                    }
                    // 得到过期日期和今天的差值(单位：天)
                    String datePoor = DateUtils.getDatePoor(endDate, new Date());
                    Integer lastDays = Integer.parseInt(datePoor.split("天")[0]);
                    expiredWarnningList.setLastDays(lastDays);
                    resultList.add(expiredWarnningList);
                }catch (Exception e){
                    log.error(e.toString());
                }
            }
            // 按照剩余时间正序排序
            resultList = resultList.stream().sorted(Comparator.comparing(ExpiredWarnningList::getLastDays)).collect(Collectors.toList());
            return resultList;
        } catch (Exception e) {
            log.error(e.toString());
        }
        return null;
    }

    public AjaxResult expiredWarnningDelete(Integer id) {
        try {
            ToolsExpiredWarnning toolsExpiredWarnning = toolsExpiredWarnningMapper.selectById(id);
            toolsExpiredWarnning.setState(1);
            toolsExpiredWarnningMapper.updateById(toolsExpiredWarnning);
            return AjaxResult.success();
        } catch (Exception e) {
            log.error(e.toString());
        }
        return AjaxResult.error();
    }
}
